<?php

namespace App\Http\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class LeapJumpRank extends Model
{
    /**
     * 设置排行
     *
     * @param [type] $uid
     * @param [type] $score
     * @return void
     */
    public static function set($uid, $score) {
        Redis::ZADD('LeapJumpRank', $score, $uid);
    }

    /**
     * 获取排行列表
     *
     * @param [type] $start
     * @param [type] $end
     * @return []
     */
    public static function gets($start, $end, $is_lw = false) {
        $key = 'LeapJumpRank';
        return Redis::ZREVRANGE($key, $start, $end, ['withscores'=>true]);
    }

    /**
     * 获取某一个用户的排名和分数
     *
     * @param [type] $uid
     * @return void
     */
    public static function getOne($uid, $is_lw = false) {
        $key = 'LeapJumpRank';
        $index = Redis::ZREVRANK($key, $uid);
        if(is_numeric($index)) $index++;
        else $index = 0;

        $score = intval(Redis::ZSCORE($key, $uid));

        return [ 'index' => $index, 'score' => $score ];
    }
}
